package com.dfdchem.imd.kaiwu.interfaces.impl;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.dfdchem.imd.kaiwu.activity.LoginActivity;
import com.dfdchem.imd.kaiwu.activity.LoginRequiredBroadcast;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityAnon;
import com.dfdchem.imd.kaiwu.application.KWApp;
import com.dfdchem.imd.kaiwu.interfaces.OnHttpResponseListener;
import com.dfdchem.imd.kaiwu.model.Resp;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.Log;

/**
 * @author yqq
 * 网络请求的Impl类，用于拦截403
 */
public class HttpResponseListenerImpl implements OnHttpResponseListener {
    private final static String TAG = "HttpResponseListenerImpl";

    private OnHttpResponseListener httpResponseListener;

    public HttpResponseListenerImpl(OnHttpResponseListener httpResponseListener) {
        this.httpResponseListener = httpResponseListener;
    }

    @SuppressLint("ShowToast")
    @Override
    public void onHttpResponse(int requestCode, String resultJson, Exception e) {
        Log.d(TAG, "onHttpResponse requestCode:" + requestCode + " resultJson:" + resultJson);
        Resp<Object> resp = JSON.parseResp(resultJson, Object.class);
        if (resp != null && resp.getCode() == 403) {

            Activity currentActivity = KWApp.getInstance().getCurrentActivity();
            if (currentActivity != null){
                if (currentActivity instanceof BaseActivityAnon) {
                    ((BaseActivityAnon) currentActivity).dismissProgressDialog();
                }
                Toast.makeText(currentActivity, "登录已过期，请重新登录", Toast.LENGTH_LONG).show();

                // 这里只负责启动 LoginActivity, 清除 X-Token 在 LoginActivity 初始化的时候做
                Intent intent = new Intent(currentActivity, LoginActivity.class);
                currentActivity.startActivity(intent);

                // 发送 LoginRequiredBroadcast.NAME 广播，已启动的需要登录的 Activity 调用 finish
                Intent intent_finish = new Intent();
                intent_finish.setAction(LoginRequiredBroadcast.NAME);
                currentActivity.sendBroadcast(intent_finish);
            } else {
                e = new Exception("请检查KWApp currentActivity实现 位于：" + TAG);
            }
            return;
        }

        // TEST
        if (requestCode == 403) {

            Activity currentActivity = KWApp.getInstance().getCurrentActivity();
            if (currentActivity != null){
                if (currentActivity instanceof BaseActivityAnon) {
                    ((BaseActivityAnon) currentActivity).dismissProgressDialog();
                }
                Toast.makeText(currentActivity, "登录已过期，请重新登录", Toast.LENGTH_LONG).show();

                // 这里只负责启动 LoginActivity, 清除 X-Token 在 LoginActivity 初始化的时候做
                Intent intent = new Intent(currentActivity, LoginActivity.class);
                currentActivity.startActivity(intent);

                // 发送 LoginRequiredBroadcast.NAME 广播，已启动的需要登录的 Activity 调用 finish
                Intent intent_finish = new Intent();
                intent_finish.setAction(LoginRequiredBroadcast.NAME);
                currentActivity.sendBroadcast(intent_finish);
            } else {
                e = new Exception("请检查KWApp currentActivity实现 位于：" + TAG);
            }
            return;
        }

        if (this.httpResponseListener != null) {
            httpResponseListener.onHttpResponse(requestCode, resultJson, e);
        }
    }
}
